<?php
/* SVN FILE: $Id: buy.php 222 2009-09-16 20:07:22Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Buy Items
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 222 $
* @lastrevision $Date: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-09-16 20:07:22 +0000 (Wed, 16 Sep 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/modules_public/shop/buy.php $
*/

class public_ibmarket_shop_buy extends marketPublicCommand
{
	public function doExecute( ipsRegistry $registry )
	{
		if($this->memberData['member_id'] == 0 || !$this->memberData['g_market_use'])
		{
			$this->registry->getClass('output')->showError('no_permission');
		}
		
		$this->request['category'] = is_array($this->caches['market_category'][$this->request['category']]) ? $this->request['category'] : -1;
		$this->request['sub'] = isset($this->request['sub']) ? intval($this->request['sub']) : ($this->request['category'] == -1 ? 1 : 0);
		
		$inventoryByID = array();
		foreach(marketRegistry::getInventoryItem($this->memberData['member_id']) as $invItem)
		{
			$inventoryByID[$invItem['item']][] = $invItem;
		}
		
		$checkoutItems = array();
		$total = array(
						'quanity' => 0,
						'price' => 0,
						'tax' => 0,
					);
		foreach($this->request['buy'] as $item_id => $quanity)
		{
			if(!is_array($this->caches['market_items'][$item_id]) || $this->caches['market_items'][$item_id]['hide_item'] || in_array($this->memberData['member_group_id'],$this->caches['market_items'][$item_id]['limit_buy']))
			{
				continue;
			}
			$quanity = numbers::parseInt($quanity);
			if(numbers::compare($this->caches['market_items'][$item_id]['stock']) >= 0 && numbers::compare($this->caches['market_items'][$item_id]['stock'],$quanity) < 0)
			{
				$quanity = $this->caches['market_items'][$item_id]['stock'];
			}
			if($this->caches['market_items'][$item_id]['inventory_limit'] && numbers::compare($this->caches['market_items'][$item_id]['inventory_limit'],numbers::add($quanity,count($inventoryByID[$item_id]))) < 0)
			{
				$quanity = numbers::subtract($this->caches['market_items'][$item_id]['inventory_limit'],count($inventoryByID[$item_id]));
			}
			if(numbers::compare($quanity) <= 0)
			{
				continue;
			}
			$item = $this->caches['market_items'][$item_id];
			$item['buy'] = array();
			$item['buy']['quanity'] = $quanity;
			$item['buy']['subtotal'] = numbers::multiply($item['price'],$quanity);
			$item['buy']['totaltax'] = numbers::divide(numbers::multiply($item['total_tax'],$item['buy']['subtotal']),100);
			$checkoutItems[] = $item;
			
			$total['quanity'] = numbers::add($total['quanity'],$item['buy']['quanity']);
			$total['price'] = numbers::add($total['price'],$item['buy']['subtotal']);
			$total['tax'] = numbers::add($total['tax'],$item['buy']['totaltax']);
		}
		$total['total'] = numbers::add($total['price'],$total['tax']);
		$total['inventory'] = count(marketRegistry::getInventoryItem($this->memberData['member_id'])) + $total['quanity'];
		
		if(count($checkoutItems) == 0)
		{
			$this->registry->getClass('output')->silentRedirect($this->settings['base_url_with_app']."module=shop");
		}
		
		if(numbers::compare($this->memberData[$this->settings['market_members_field']],$total['total']) < 0)
		{
			$this->registry->getClass('output')->showError('checkout_not_enough');
		}
		
		if($this->settings['market_inventory_max'] && $total['inventory'] > $this->settings['market_inventory_max'])
		{
			$this->registry->getClass('output')->showError('checkout_too_many_items');
		}
		
		foreach($checkoutItems as $item)
		{
			$count = 0;
			
			$expired = $item['expire'] ? IPS_UNIX_TIME_NOW + $item['expire'] : 0;
			
			for($i = 0; $i < $item['buy']['quanity']; $i++)
			{
				$invItem = array(
									'member_id' => $this->memberData['member_id'],
									'item' => $item['item_id'],
									'price_bought' => $item['price'],
									'resell_price' => $item['resell'],
									'time_bought' => IPS_UNIX_TIME_NOW,
									'settings' => '',
									'expired' => $expired,
								);
				if(marketRegistry::getItem($item)->publicOnCreateItem($invItem))
				{
					$this->DB->insert('market_inventory',$invItem);
					$count++;
				}
			}
			
			if($count == 0)
			{
				continue;
			}
			
			if(numbers::compare($item['stock']) > 0)
			{
				$stock = numbers::subtract($item['stock'], $count);
				$restock = $item['restock_time'];
				if(($item['restock'] == 1 || $item['restock'] == 2) && !$restock)
				{
					$_item = $item;
					$_item['stock'] = $stock;
					$restock = marketRegistry::nextRestockTime($_item);
				}
				$this->DB->update('market_items',array('stock' => $stock, 'restock_time' => $restock),"item_id=".$item['item_id']);
			}
			
			$this->DB->insert('market_buy_logs',array(
														'member_id' => $this->memberData['member_id'],
														'method' => 'buy',
														'item_id' => $item['item_id'],
														'ip_address' => $this->member->ip_address,
														'time_bought' => IPS_UNIX_TIME_NOW,
														'quanity' => $count,
														'price_bought' => $item['price'],
														'resell_price' => $item['resell'],
														'tax_total' => $item['tax_amount'],
														'category' => $item['category'],
														'item_name' => $item['item_name'],
														'item_description' => marketRegistry::getItem($item)->parseStoreDescription(),
														'icon' => $item['icon'],
														'category_name' => $this->caches['market_category'][$item['category']]['cat_name'],
							)						);
		}
		
		IPSMember::save( $this->memberData['member_id'], array( 'core' => array( $this->settings['market_members_field'] => numbers::subtract($this->memberData[$this->settings['market_members_field']],$total['total']) ) ) );
		
		marketRegistry::recordFee($total['tax']);
		
		$this->cache->rebuildCache('market_items','ibmarket');
		
		$this->registry->getClass('output')->redirectScreen( $this->lang->words['bought_items'], $this->settings['base_url_with_app'] . "module=inventory" );
	}
}